package org.zxyj.manage.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zxyj.core.constants.Constants;
import org.zxyj.core.constants.Keys;
import org.zxyj.core.entity.Pagination;
import org.zxyj.core.entity.Result;
import org.zxyj.core.errorno.SystemErrorNo;
import org.zxyj.core.exception.ServiceException;
import org.zxyj.core.exception.UnifyException;
import org.zxyj.core.utils.DateUtil;
import org.zxyj.core.web.controller.BaseController;
import org.zxyj.manage.entity.Batch;
import org.zxyj.manage.service.IBatchService;
import org.zxyj.system.server.ISysParamsService;

/**
 * 阅卷批次设置
    * @ClassName: BatchController
    * @author liuyong
    * @date 2016年7月1日
 */
@Controller
@RequestMapping("/manage/batch")
public class BatchController extends BaseController{
	@Autowired
	private IBatchService batchService;
	
	@Autowired
	private ISysParamsService sysParamsService;
	
	@RequestMapping({"","/","/index"})
	public ModelAndView index(ModelAndView mv) {
		mv.setViewName(Views.LIST_VIEW);
		Map<String, Object> criteria = new HashMap<String, Object>();
		Object query_mc = getParaValue("query_mc");
		Object query_sfsy = getParaValue("query_sfsy");
		Object query_ksrq = getParaValue("query_ksrq");
		Object query_jsrq = getParaValue("query_jsrq");
		criteria.put("mc", query_mc);
		criteria.put("sfsy", query_sfsy);
		criteria.put("query_ksrq", query_ksrq);
		criteria.put("query_jsrq", query_jsrq);
		Pagination page = (Pagination) getRequestAttr("pagination");
		Pagination pagination = batchService.listEntities(criteria, page);
		mv.addObject(Keys.KEY_PAGINATION, pagination);
		mv.addObject("query_mc", query_mc);
		mv.addObject("query_sfsy", query_sfsy);
		mv.addObject("query_ksrq", query_ksrq !=null?DateUtil.parse(query_ksrq+"", null):null);
		mv.addObject("query_jsrq", query_ksrq !=null?DateUtil.parse(query_jsrq+"", null):null);
		mv.addObject("sfsys", sysParamsService.listEntitiesByKey(Keys.STATUS));
		return mv;
	}
	
	@RequestMapping("/add")
	public ModelAndView add(ModelAndView mv, Batch entity) {
		mv.setViewName(Views.ADD_VIEW);
		mv.addObject(Keys.KEY_PARAM, entity);
		mv.addObject("sfsys", sysParamsService.listEntitiesByKey(Keys.STATUS));
		return mv;
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(ModelAndView mv, Batch entity) {
		if (null == entity || null == entity.getId()) {
			throw new UnifyException(SystemErrorNo.DATA_NOT_EXIST);
		}
		mv.setViewName(Views.EDIT_VIEW);
		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("id", entity.getId());
		entity = batchService.findEntity(criteria);
		if (null == entity || null == entity.getId()) {
			throw new UnifyException(SystemErrorNo.DATA_NOT_EXIST);
		}
		mv.addObject(Keys.KEY_PARAM, entity);
		mv.addObject("sfsys", sysParamsService.listEntitiesByKey(Keys.STATUS));
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/edit/exec")
	public Result<?> doEdit(Batch entity) throws Exception {
    	Result<?> result = new Result<String>();
    	try{
    		this.batchService.saveOrUpdateEntity(entity);
    		result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
    	return result;
	}
	
	@ResponseBody
	@RequestMapping("/delete/exec")
	public Result<?> doDelete(Batch entity) {
		Result<?> result = new Result<String>();
		try {
			Map<String,Object> criteria = new HashMap<String,Object>();
        	criteria.put("id", entity.getId());
			this.batchService.deleteEntity(criteria);
			result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
		return result;
	}
	
	class Views {
		/**
		 * index视图
		 */
		public static final String LIST_INDEX = "/manage/batch/list";
		
		/**
		 * 列表视图
		 */
		public static final String LIST_VIEW = "/manage/batch/list";

		/**
		 * 添加视图
		 */
		public static final String ADD_VIEW = "/manage/batch/edit";

		/**
		 * 编辑视图
		 */
		public static final String EDIT_VIEW = "/manage/batch/edit";
	}
}
